const pool = require('../config/db');
const bcrypt = require('bcryptjs');

async function createTestMerchant() {
    const connection = await pool.getConnection();
    try {
        await connection.beginTransaction();

        // 1. 创建商户用户
        const password = '123456';
        const hashedPassword = await bcrypt.hash(password, 10);
        
        const [userResult] = await connection.execute(
            'INSERT INTO users (phone, password, role, name) VALUES (?, ?, ?, ?)',
            ['17590530190', hashedPassword, 'store', '测试商户']
        );

        const userId = userResult.insertId;
        console.log('创建商户用户成功，ID:', userId);

        // 2. 创建店铺信息
        const [storeResult] = await connection.execute(
            `INSERT INTO stores (owner_id, name, type, address, contact_phone, description) 
             VALUES (?, ?, ?, ?, ?, ?)`,
            [
                userId,
                '测试店铺',
                '美食',
                '测试地址',
                '17590530190',
                '这是一个用于测试的店铺'
            ]
        );

        console.log('创建店铺成功，ID:', storeResult.insertId);
        
        await connection.commit();
        console.log('测试商户创建成功！');
        console.log('登录信息：');
        console.log('手机号：17590530190');
        console.log('密码：123456');
        
    } catch (error) {
        await connection.rollback();
        console.error('创建测试商户失败:', error);
    } finally {
        connection.release();
        process.exit();
    }
}

createTestMerchant(); 